home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JDPIPE.C < prev    next >
C/C++ Source or Header  |  1992-11-10  |  39KB  |  1,049 lines

  1. /*
  2.  * jdpipe.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains decompression pipeline controllers.
  9.  * These routines are invoked via the d_pipeline_controller method.
  10.  *
  11.  * There are two basic pipeline controllers.  The simpler one handles a
  12.  * single-scan JPEG file (single component or fully interleaved) with no
  13.  * color quantization or 1-pass quantization.  In this case, the file can
  14.  * be processed in one top-to-bottom pass.  The more complex controller is
  15.  * used when 2-pass color quantization is requested and/or the JPEG file
  16.  * has multiple scans (noninterleaved or partially interleaved).  In this
  17.  * case, the entire image must be buffered up in a "big" array.
  18.  *
  19.  * If you need to make a minimal implementation, the more complex controller
  20.  * can be compiled out by disabling the appropriate configuration options.
  21.  * We don't recommend this, since then you can't handle all legal JPEG files.
  22.  */
  23.  
  24. #include "jinclude.h"
  25.  
  26.  
  27. #ifdef D_MULTISCAN_FILES_SUPPORTED /* wish we could assume ANSI's defined() */
  28. #define NEED_COMPLEX_CONTROLLER
  29. #else
  30. #ifdef QUANT_2PASS_SUPPORTED
  31. #define NEED_COMPLEX_CONTROLLER
  32. #endif
  33. #endif
  34.  
  35.  
  36. /*
  37.  * About the data structures:
  38.  *
  39.  * The processing chunk size for upsampling is referred to in this file as
  40.  * a "row group": a row group is defined as Vk (v_samp_factor) sample rows of
  41.  * any component while downsampled, or Vmax (max_v_samp_factor) unsubsampled
  42.  * rows.  In an interleaved scan each MCU row contains exactly DCTSIZE row
  43.  * groups of each component in the scan.  In a noninterleaved scan an MCU row
  44.  * is one row of blocks, which might not be an integral number of row groups;
  45.  * therefore, we read in Vk MCU rows to obtain the same amount of data as we'd
  46.  * have in an interleaved scan.
  47.  * To provide context for the upsampling step, we have to retain the last
  48.  * two row groups of the previous MCU row while reading in the next MCU row
  49.  * (or set of Vk MCU rows).  To do this without copying data about, we create
  50.  * a rather strange data structure.  Exactly DCTSIZE+2 row groups of samples
  51.  * are allocated, but we create two different sets of pointers to this array.
  52.  * The second set swaps the last two pairs of row groups.  By working
  53.  * alternately with the two sets of pointers, we can access the data in the
  54.  * desired order.
  55.  *
  56.  * Cross-block smoothing also needs context above and below the "current" row.
  57.  * Since this is an optional feature, I've implemented it in a way that is
  58.  * much simpler but requires more than the minimum amount of memory.  We
  59.  * simply allocate three extra MCU rows worth of coefficient blocks and use
  60.  * them to "read ahead" one MCU row in the file.  For a typical 1000-pixel-wide
  61.  * image with 2x2,1x1,1x1 sampling, each MCU row is about 50Kb; an 80x86
  62.  * machine may be unable to apply cross-block smoothing to wider images.
  63.  */
  64.  
  65.  
  66. /*
  67.  * These variables are logically local to the pipeline controller,
  68.  * but we make them static so that scan_big_image can use them
  69.  * without having to pass them through the quantization routines.
  70.  */
  71.  
  72. static int rows_in_mem;        /* # of sample rows in full-size buffers */
  73. /* Work buffer for data being passed to output module. */
  74. /* This has color_out_comps components if not quantizing, */
  75. /* but only one component when quantizing. */
  76. static JSAMPIMAGE output_workspace;
  77.  
  78. #ifdef NEED_COMPLEX_CONTROLLER
  79. /* Full-size image array holding upsampled, but not color-processed data. */
  80. static big_sarray_ptr *fullsize_image;
  81. static JSAMPIMAGE fullsize_ptrs; /* workspace for access_big_sarray() result */
  82. #endif
  83.  
  84.  
  85. /*
  86.  * Utility routines: common code for pipeline controllers
  87.  */
  88.  
  89. LOCAL void
  90. interleaved_scan_setup (decompress_info_ptr cinfo)
  91. /* Compute all derived info for an interleaved (multi-component) scan */
  92. /* On entry, cinfo->comps_in_scan and cinfo->cur_comp_info[] are set up */
  93. {
  94.   short ci, mcublks;
  95.   jpeg_component_info *compptr;
  96.  
  97.   if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  98.     ERREXIT(cinfo->emethods, "Too many components for interleaved scan");
  99.  
  100.   cinfo->MCUs_per_row = (cinfo->image_width
  101.              + cinfo->max_h_samp_factor*DCTSIZE - 1)
  102.             / (cinfo->max_h_samp_factor*DCTSIZE);
  103.  
  104.   cinfo->MCU_rows_in_scan = (cinfo->image_height
  105.                  + cinfo->max_v_samp_factor*DCTSIZE - 1)
  106.                 / (cinfo->max_v_samp_factor*DCTSIZE);
  107.   
  108.   cinfo->blocks_in_MCU = 0;
  109.  
  110.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  111.     compptr = cinfo->cur_comp_info[ci];
  112.     /* for interleaved scan, sampling factors give # of blocks per component */
  113.     compptr->MCU_width = compptr->h_samp_factor;
  114.     compptr->MCU_height = compptr->v_samp_factor;
  115.     compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  116.     /* compute physical dimensions of component */
  117.     compptr->downsampled_width = jround_up(compptr->true_comp_width,
  118.                        (long) (compptr->MCU_width*DCTSIZE));
  119.     compptr->downsampled_height = jround_up(compptr->true_comp_height,
  120.                         (long) (compptr->MCU_height*DCTSIZE));
  121.     /* Sanity check */
  122.     if (compptr->downsampled_width !=
  123.     (cinfo->MCUs_per_row * (compptr->MCU_width*DCTSIZE)))
  124.       ERREXIT(cinfo->emethods, "I'm confused about the image width");
  125.     /* Prepare array describing MCU composition */
  126.     mcublks = compptr->MCU_blocks;
  127.     if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  128.       ERREXIT(cinfo->emethods, "Sampling factors too large for interleaved scan");
  129.     while (mcublks-- > 0) {
  130.       cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  131.     }
  132.   }
  133.  
  134.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  135. }
  136.  
  137.  
  138. LOCAL void
  139. noninterleaved_scan_setup (decompress_info_ptr cinfo)
  140. /* Compute all derived info for a noninterleaved (single-component) scan */
  141. /* On entry, cinfo->comps_in_scan = 1 and cinfo->cur_comp_info[0] is set up */
  142. {
  143.   jpeg_component_info *compptr = cinfo->cur_comp_info[0];
  144.  
  145.   /* for noninterleaved scan, always one block per MCU */
  146.   compptr->MCU_width = 1;
  147.   compptr->MCU_height = 1;
  148.   compptr->MCU_blocks = 1;
  149.   /* compute physical dimensions of component */
  150.   compptr->downsampled_width = jround_up(compptr->true_comp_width,
  151.                      (long) DCTSIZE);
  152.   compptr->downsampled_height = jround_up(compptr->true_comp_height,
  153.                       (long) DCTSIZE);
  154.  
  155.   cinfo->MCUs_per_row = compptr->downsampled_width / DCTSIZE;
  156.   cinfo->MCU_rows_in_scan = compptr->downsampled_height / DCTSIZE;
  157.  
  158.   /* Prepare array describing MCU composition */
  159.   cinfo->blocks_in_MCU = 1;
  160.   cinfo->MCU_membership[0] = 0;
  161.  
  162.   (*cinfo->methods->d_per_scan_method_selection) (cinfo);
  163. }
  164.  
  165.  
  166.  
  167. LOCAL JSAMPIMAGE
  168. alloc_sampimage (decompress_info_ptr cinfo,
  169.          int num_comps, long num_rows, long num_cols)
  170. /* Allocate an in-memory sample image (all components same size) */
  171. {
  172.   JSAMPIMAGE image;
  173.   int ci;
  174.  
  175.   image = (JSAMPIMAGE) (*cinfo->emethods->alloc_small)
  176.                 (num_comps * SIZEOF(JSAMPARRAY));
  177.   for (ci = 0; ci < num_comps; ci++) {
  178.     image[ci] = (*cinfo->emethods->alloc_small_sarray) (num_cols, num_rows);
  179.   }
  180.   return image;
  181. }
  182.  
  183.  
  184. #if 0                /* this routine not currently needed */
  185.  
  186. LOCAL void
  187. free_sampimage (decompress_info_ptr cinfo, JSAMPIMAGE image, int num_comps)
  188. /* Release a sample image created by alloc_sampimage */
  189. {
  190.   int ci;
  191.  
  192.   for (ci = 0; ci < num_comps; ci++) {
  193.       (*cinfo->emethods->free_small_sarray) (image[ci]);
  194.   }
  195.   (*cinfo->emethods->free_small) ((void *) image);
  196. }
  197.  
  198. #endif
  199.  
  200.  
  201. LOCAL JBLOCKIMAGE
  202. alloc_MCU_row (decompress_info_ptr cinfo)
  203. /* Allocate one MCU row's worth of coefficient blocks */
  204. {
  205.   JBLOCKIMAGE image;
  206.   int ci;
  207.  
  208.   image = (JBLOCKIMAGE) (*cinfo->emethods->alloc_small)
  209.                 (cinfo->comps_in_scan * SIZEOF(JBLOCKARRAY));
  210.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  211.     image[ci] = (*cinfo->emethods->alloc_small_barray)
  212.             (cinfo->cur_comp_info[ci]->downsampled_width / DCTSIZE,
  213.              (long) cinfo->cur_comp_info[ci]->MCU_height);
  214.   }
  215.   return image;
  216. }
  217.  
  218.  
  219. #ifdef NEED_COMPLEX_CONTROLLER    /* not used by simple cont